import * as $http from '@/utlis/interface/orderDlatils'
import { ref, reactive, } from 'vue'
import { NewOrder, useStore } from '@/Pinia/index'
import * as $httphome from "@/utlis/interface/home";

interface pickCdoeDta {
	orderId: string | number,
	pickCode: any
}

// 跑腿订单取货码
const takeGoodInfo = reactive<pickCdoeDta>({
	orderId: '',
	pickCode: null
})
export default (proxy: any, Emit: any, Props: any) => {
	const nowstatus = useStore()
	const butChange = reactive<any>({
		title: '抢单',
		btn_disable: true,
		data: {
			riderLongitude: null,
			riderLatitude: null,
			orderId: 0
		}
	})
	//跑腿抢单
	const makeOrder = (id: number | string, index: number) => {
		if (nowstatus.infoData.userRealNameStatus === 1) {
			let data = ''
			$http.runErrandsMakeOrder(data, id, proxy).then((res: any) => {
				if (res.code == 0) {
					proxy.$API(proxy).Toast(res.msg, 'success')
					setTimeout(() => {
						// Emit('makeOrder')
						//待取货
						Emit('changeTab', { index: 1 });
						getNewOrder();
					}, 1000)
				}
			}).catch((err: any) => {
				proxy.$API(proxy).Toast(err.msg, 'success')
			})
		} else {
			Emit('setSkipModel')
		}
	}
	const callPhone = (index: number, people: string) => {
		if (people == "联系发件人") {
			let phone = Props.orderlist[index].pickPhone
			if (phone) {
				uni.makePhoneCall({
					phoneNumber: phone,
				})
			}
		} else {
			let phone = Props.orderlist[index].receivePhone
			if (phone) {
				uni.makePhoneCall({
					phoneNumber: phone,
				})
			}
		}

	}
	const takeGoods = (id: string | number, currentStatus: number, riderDo: number | string) => {
		let data = { id, riderDo }
		let takeInfo = {
			pickCode: null,
			orderId: id,
			riderLongitude: uni.getStorageSync('riderLongitude'),
			riderLatitude: uni.getStorageSync('riderLatitude'),
		}
		if (nowstatus.infoData.userRealNameStatus === 1) {
			switch (currentStatus) {
				case 3:
					Emit('setSendCodeModel', data)
					break
				case 4:
					Emit('setSendCodeModel', data)
					break
				case 5:
					Emit('setSendCodeModel', data)
					break
				default:
					console.log("测试")
					$http.runErrandReceviedGoods(takeInfo, proxy).then((res: any) => {
						if (res.code == 0) {
							proxy.$API(proxy).Toast(res.msg, 'success')
							setTimeout(() => {
								Emit('makeOrder')
							}, 1000)
						}
					}).catch((err: any) => {
						proxy.$API(proxy).Toast(err.msg, 'waring')
					})

			}

		} else {
			Emit('setSkipModel')
		}
	}
	const NewOrders = NewOrder();

	//更新外卖跑腿订单数量
	const getNewOrder = () => {
		let dateTimes = new Date().getTime()
		$httphome.queryNewOrdersReuqest({ 'time': dateTimes }, proxy).then((res: any) => {
			dateTimes = res.data.time
			NewOrders.NewOrders.order0 = res.data.order0
			NewOrders.NewOrders.order1 = res.data.order1
		})
	}

	// 显示转单按钮弹出层
	const clickIsShow = (item: any) => {
		if (item.riderClientStatus == 1 && item.status == 1) {
			Emit('clickIsShow', item.id)
		} else {
			// return 	proxy.$API(proxy).Toast('当前订单不可转单', 'error')
		}
	}

	// 获取通话列表按钮
	const getPhoneList = (item: any) => {
		if (item.riderClientStatus == 4) {
			return
		}

		let obj = {}

		if (item.riderDo == '1') {
			obj = {
				type: item.riderClientStatus,
				riderPhone: item.riderPhone,
				orderId: item.id,
				arr: [{
					title: '联系收件人',
					phone: item.receivePhone,
					type: "C1"
				}, {
					title: '注册用户',
					phone: item.userMobile,
					type: "C0"
				},]
			}
		} else {
			obj = {
				type: item.riderClientStatus,
				riderPhone: item.riderPhone,
				orderId: item.id,
				arr: [
					{
						title: '联系发件人',
						phone: item.pickPhone,
						type: "C1"
					},
					{
						title: '联系收件人',
						phone: item.receivePhone,
						type: "C2"
					}, {
						title: '注册用户',
						phone: item.userMobile,
						type: "C0"
					},
				]
			}
		}

		Emit('getPhoneList', obj)
	}

	return {
		makeOrder,
		butChange,
		callPhone,
		takeGoods,
		clickIsShow,
		getPhoneList,
	}


}
